unit EngDbMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls,
  Vcl.Imaging.pngimage, System.Actions, Vcl.ActnList, Vcl.Touch.GestureMgr,
  Data.DB, Data.Win.ADODB, Vcl.Mask, Vcl.DBCtrls, Vcl.Grids, Vcl.DBGrids,
  Vcl.Menus, Vcl.ComCtrls, frxClass, frxDBSet, frxDesgn, frxDock, frxExportText,
  frxExportPDF, frxExportMail;

const
  _TBLTASK = 0;
  _TBLPRJ = 1;

type
  TfrmEngDb = class(TForm)
    AppBar: TPanel;
    CloseButton: TImage;
    ActionList1: TActionList;
    Action1: TAction;
    connEngDb: TADOConnection;
    tblTask: TADOTable;
    dsTask: TDataSource;
    dsSubTask: TDataSource;
    tblSubTask: TADOTable;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    PageControl1: TPageControl;
    tsTasks: TTabSheet;
    pnlSubTask: TPanel;
    navSubTask: TDBNavigator;
    Label8: TLabel;
    dbGridSubTasks: TDBGrid;
    DBText3: TDBText;
    Label7: TLabel;
    Label10: TLabel;
    DBText4: TDBText;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    dtSubTaskDue: TDateTimePicker;
    DBMemo3: TDBMemo;
    DBComboBox2: TDBComboBox;
    dbGridTasks: TDBGrid;
    Label15: TLabel;
    DBMemo4: TDBMemo;
    bevTask: TBevel;
    cbViewSubTaskControls: TCheckBox;
    tsResources: TTabSheet;
    tblResource: TADOTable;
    pnlResource: TPanel;
    tblResourceRES_ID: TAutoIncField;
    tblResourceLAST_NAME: TWideStringField;
    tblResourceFIRST_NAME: TWideStringField;
    tblResourceFULL_NAME: TWideStringField;
    tblResourceTECH_TEAM: TWideStringField;
    tblResourceCOMPANY: TWideStringField;
    tblResourceWORK_PHONE: TWideStringField;
    tblResourceCELL_PHONE: TWideStringField;
    tblResourceHOME_PHONE: TWideStringField;
    tblResourceEMAIL: TWideStringField;
    tblResourceADDRESS1: TWideStringField;
    tblResourceADDRESS2: TWideStringField;
    tblResourceCITY: TWideStringField;
    tblResourceSTATE: TWideStringField;
    tblResourceCOUNTRY: TWideStringField;
    Label16: TLabel;
    DBEdit2: TDBEdit;
    dsResource: TDataSource;
    Label17: TLabel;
    DBEdit4: TDBEdit;
    DBText5: TDBText;
    Label18: TLabel;
    DBEdit5: TDBEdit;
    Label19: TLabel;
    DBEdit6: TDBEdit;
    Label20: TLabel;
    DBEdit7: TDBEdit;
    Label21: TLabel;
    DBEdit8: TDBEdit;
    Label22: TLabel;
    DBEdit9: TDBEdit;
    Label23: TLabel;
    DBEdit10: TDBEdit;
    Label24: TLabel;
    DBEdit11: TDBEdit;
    Label25: TLabel;
    DBEdit12: TDBEdit;
    Label26: TLabel;
    DBEdit13: TDBEdit;
    Label27: TLabel;
    DBEdit14: TDBEdit;
    Label28: TLabel;
    DBEdit15: TDBEdit;
    Label29: TLabel;
    DBEdit16: TDBEdit;
    tblResourceDEPT: TWideStringField;
    cbViewTaskControls: TCheckBox;
    dbGridResource: TDBGrid;
    tsProjects: TTabSheet;
    pnlPrj: TPanel;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    DBText7: TDBText;
    Label37: TLabel;
    DBNavigator1: TDBNavigator;
    DBMemo5: TDBMemo;
    DBMemo6: TDBMemo;
    dtPrjDue: TDateTimePicker;
    DBComboBox3: TDBComboBox;
    tblPrj: TADOTable;
    dsPrj: TDataSource;
    Label38: TLabel;
    DBEdit18: TDBEdit;
    Label39: TLabel;
    dbGridPrj: TDBGrid;
    tblPrjTask: TADOTable;
    dsPrjTask: TDataSource;
    dbGridPrjTask: TDBGrid;
    pnlPrjTask: TPanel;
    DBText8: TDBText;
    Label40: TLabel;
    Label41: TLabel;
    DBText9: TDBText;
    Label42: TLabel;
    Label43: TLabel;
    Label44: TLabel;
    Label45: TLabel;
    Label46: TLabel;
    DBNavigator2: TDBNavigator;
    dtPrjTaskDue: TDateTimePicker;
    DBComboBox4: TDBComboBox;
    DBMemo8: TDBMemo;
    cbViewPrjTaskControls: TCheckBox;
    Label47: TLabel;
    bevPrj: TBevel;
    DBMemo7: TDBMemo;
    tblPrjTaskPRJTASK_ID: TAutoIncField;
    tblPrjTaskPRJ_PARENT: TIntegerField;
    tblPrjTaskCREATION_DATE: TDateTimeField;
    tblPrjTaskOWNER: TWideStringField;
    tblPrjTaskDESCRIPTION: TWideStringField;
    tblPrjTaskSTATUS: TWideStringField;
    tblPrjTaskDUE_DATE: TDateTimeField;
    tblPrjTaskNOTES: TWideMemoField;
    tblPrjPRJ_ID: TAutoIncField;
    tblPrjCREATION_DATE: TDateTimeField;
    tblPrjDESCRIPTION: TWideStringField;
    tblPrjOWNER: TWideStringField;
    tblPrjPRJ_TT: TWideStringField;
    tblPrjSTATUS: TWideStringField;
    tblPrjDUE_DATE: TDateTimeField;
    tblSubTaskSUBTASK_ID: TAutoIncField;
    tblSubTaskPARENT_TASK: TIntegerField;
    tblSubTaskCREATION_DATE: TDateTimeField;
    tblSubTaskOWNER: TWideStringField;
    tblSubTaskDUE_DATE: TDateTimeField;
    tblSubTaskDESCRIPTION: TWideStringField;
    tblSubTaskSTATUS: TWideStringField;
    tblSubTaskNOTES: TWideMemoField;
    tblTaskTASK_ID: TAutoIncField;
    tblTaskCREATION_DATE: TDateTimeField;
    tblTaskOWNER: TWideStringField;
    tblTaskDESCRIPTION: TWideStringField;
    tblTaskSTATUS: TWideStringField;
    tblTaskDUE_DATE: TDateTimeField;
    tblTaskNOTES: TWideMemoField;
    cbViewCtrlsResources: TCheckBox;
    tblResourceGROUP: TWideStringField;
    tblResourceNOTES: TWideMemoField;
    DBMemo9: TDBMemo;
    Label48: TLabel;
    DBComboBox6: TDBComboBox;
    DBComboBox7: TDBComboBox;
    DBComboBox8: TDBComboBox;
    tsReport: TTabSheet;
    DBNavigator3: TDBNavigator;
    Label49: TLabel;
    btnReport: TButton;
    dsRep: TDataSource;
    qRep: TADOQuery;
    lblSubTaskState: TLabel;
    lblResourceState: TLabel;
    lblPrjTaskState: TLabel;
    lblPrjState: TLabel;
    frxDBDataset1: TfrxDBDataset;
    frxDBDataset2: TfrxDBDataset;
    frxDBDataset3: TfrxDBDataset;
    frxDBDataset4: TfrxDBDataset;
    frxDBDataset5: TfrxDBDataset;
    pnlTask: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    DBText1: TDBText;
    DBText2: TDBText;
    Label9: TLabel;
    lblTaskTblState: TLabel;
    navTask: TDBNavigator;
    DBMemo1: TDBMemo;
    DBMemo2: TDBMemo;
    dtTaskDue: TDateTimePicker;
    DBComboBox1: TDBComboBox;
    DBComboBox5: TDBComboBox;
    tblReport: TADOTable;
    dsReportTbl: TDataSource;
    frxReport1: TfrxReport;
    btnSql: TButton;
    tblPrjNOTES: TWideMemoField;
    frxDBDataset6: TfrxDBDataset;
    dbGridRep: TDBGrid;
    tblReportID: TAutoIncField;
    tblReportRPTNAME: TWideStringField;
    tblReportDESCRIPTION: TWideStringField;
    tblReportSQLSTRING: TWideMemoField;
    tblReportRPTPATH: TWideStringField;
    btnRefreshRpt: TButton;
    frxPDFExport1: TfrxPDFExport;
    lblProjectControlsExit: TLabel;
    View1: TMenuItem;
    ViewProjectControls1: TMenuItem;
    btnSaveSql: TButton;
    edFilter: TEdit;
    cbFilter: TCheckBox;
    lblTableTitle: TLabel;
    dlgReportOpen: TOpenDialog;
    pnlReportControls: TPanel;
    Label50: TLabel;
    edReportName: TDBEdit;
    Label51: TLabel;
    DBEdit3: TDBEdit;
    Label53: TLabel;
    edReportLocation: TDBEdit;
    Label31: TLabel;
    btnGetPath: TButton;
    DBText6: TDBText;
    Label52: TLabel;
    memSql: TDBMemo;
    DBNavigator4: TDBNavigator;
    Label30: TLabel;
    cbShowReportControls: TCheckBox;
    btnNewReport: TButton;
    frxDesigner1: TfrxDesigner;
    memSqlTest: TMemo;
    frxMailExport1: TfrxMailExport;
    procedure CloseButtonClick(Sender: TObject);
    procedure Action1Execute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dsTaskStateChange(Sender: TObject);
    procedure tblTaskAfterInsert(DataSet: TDataSet);
    procedure tblTaskAfterScroll(DataSet: TDataSet);
    procedure tblTaskBeforePost(DataSet: TDataSet);
    procedure tblSubTaskAfterInsert(DataSet: TDataSet);
    procedure tblSubTaskAfterScroll(DataSet: TDataSet);
    procedure tblSubTaskBeforePost(DataSet: TDataSet);
    procedure tblPrjAfterInsert(DataSet: TDataSet);
    procedure tblPrjAfterScroll(DataSet: TDataSet);
    procedure tblPrjBeforePost(DataSet: TDataSet);
    procedure tblPrjTaskAfterInsert(DataSet: TDataSet);
    procedure tblPrjTaskAfterScroll(DataSet: TDataSet);
    procedure tblPrjTaskBeforePost(DataSet: TDataSet);
    procedure dtTaskDueEnter(Sender: TObject);
    procedure dtTaskDueChange(Sender: TObject);
    procedure dtSubTaskDueEnter(Sender: TObject);
    procedure dtSubTaskDueChange(Sender: TObject);
    procedure dtPrjDueEnter(Sender: TObject);
    procedure dtPrjDueChange(Sender: TObject);
    procedure dtPrjTaskDueEnter(Sender: TObject);
    procedure dtPrjTaskDueChange(Sender: TObject);
    procedure cbViewSubTaskControlsClick(Sender: TObject);
    procedure cbViewTaskControlsClick(Sender: TObject);
    procedure cbViewPrjTaskControlsClick(Sender: TObject);
    procedure tsTasksResize(Sender: TObject);
    procedure PageControl1Resize(Sender: TObject);
    procedure dbGridTasksMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure dbGridSubTasksMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure dbGridPrjMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure dbGridPrjTaskMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure dbGridPrjTaskTitleClick(Column: TColumn);
    procedure dbGridPrjTitleClick(Column: TColumn);
    procedure dbGridTasksTitleClick(Column: TColumn);
    procedure dbGridSubTasksTitleClick(Column: TColumn);
    procedure dbGridTasksDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure dbGridSubTasksDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure dbGridPrjDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure dbGridPrjTaskDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure cbViewCtrlsResourcesClick(Sender: TObject);
    procedure dbGridResourceDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBOwnerDropDown(Sender: TObject);
    procedure DBOwnerExit(Sender: TObject);
    procedure btnReportClick(Sender: TObject);
    procedure DBEdit2Exit(Sender: TObject);
    procedure dbGridResourceTitleClick(Column: TColumn);
    procedure btnSqlClick(Sender: TObject);
    procedure dsSubTaskStateChange(Sender: TObject);
    procedure dsResourceStateChange(Sender: TObject);
    procedure dsPrjTaskStateChange(Sender: TObject);
    procedure dsPrjStateChange(Sender: TObject);
    procedure btnRefreshRptClick(Sender: TObject);
    procedure lblProjectControlsExitClick(Sender: TObject);
    procedure ViewProjectControls1Click(Sender: TObject);
    procedure dbGridPrjDblClick(Sender: TObject);
    procedure btnSaveSqlClick(Sender: TObject);
    procedure dsRepUpdateData(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure cbFilterClick(Sender: TObject);
    procedure btnGetPathClick(Sender: TObject);
    procedure cbShowReportControlsClick(Sender: TObject);
    procedure btnNewReportClick(Sender: TObject);

 private
    { Private declarations }
    procedure AppBarResize;
    procedure AppBarShow(mode: integer);
    procedure SubTaskVisible(State: Boolean);
    procedure TaskVisible(State: Boolean);
    procedure PrjVisible(State: Boolean);
    procedure PrjTaskVisible(State: Boolean);
    procedure ResourceVisible(state: Boolean);
    function  dsStateChange(Sender: TObject):string;
  public
    { Public declarations }
    ActiveTbl: Integer;
  end;

var
  frmEngDb: TfrmEngDb;  pnlRptDesign: TPanel;

implementation

{$R *.dfm}

uses GridHandler, DBFilter;

const
  AppBarHeight = 75;
(******************************************************************************
  AppBarResize(), AppBarShow:   Default procedures created as part of Metropolis
                                theme. Not currently used.
*******************************************************************************)
procedure TfrmEngDb.AppBarResize;
begin
  AppBar.SetBounds(0, AppBar.Parent.Height - AppBarHeight,
    AppBar.Parent.Width, AppBarHeight);
end;

procedure TfrmEngDb.AppBarShow(mode: integer);
begin
  if mode = -1 then // Toggle
    mode := integer(not AppBar.Visible );

  if mode = 0 then
    AppBar.Visible := False
  else
  begin
    AppBar.Visible := True;
    AppBar.BringToFront;
  end;
end;


(*******************************************************************************
  ViewSubTaskControlsClick():  Determine the visual state of the SubTask
                               Controls and display accordingly.
*******************************************************************************)
procedure TfrmEngDb.cbViewSubTaskControlsClick(Sender: TObject);
begin
  if cbViewSubTaskControls.Checked then
    SubTaskVisible(True)
      else
        SubTaskVisible(False);
end;

(*******************************************************************************
  ViewTaskControlsClick():  Determine the visual state of the SubTask
                               Controls and display accordingly.
*******************************************************************************)
procedure TfrmEngDb.cbViewTaskControlsClick(Sender: TObject);
begin
  if cbViewTaskControls.Checked then
    TaskVisible(True)
      else
        TaskVisible(False);
end;

(*******************************************************************************
  Action1Execute():  Default Action Manager created by Metropolis. May remove if
                     not used in future.
 ******************************************************************************)
procedure TfrmEngDb.Action1Execute(Sender: TObject);
begin
  AppBarShow(-1);
end;
(*******************************************************************************
  CloseButtonClick():  Responds to user click of "X" window button.
*******************************************************************************)
procedure TfrmEngDb.CloseButtonClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TfrmEngDb.DBEdit2Exit(Sender: TObject);
begin
  if (tblResource.State = dsInsert) or (tblResource.State = dsEdit) then
    begin
      if (tblResource['FIRST_NAME'] <>'') and (tblResource['LAST_NAME'] <>'') then
        tblResource['FULL_NAME'] := tblResource['FIRST_NAME']+ ' ' +
        tblResource['LAST_NAME'];
    end;
end;

(*******************************************************************************
  FormClose():  Fires when form is closed. Perform all clean up work here before
                application terminates.
*******************************************************************************)
procedure TfrmEngDb.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if connEngDb.Connected then
    connEngDb.Close;
end;
(*******************************************************************************
  FormCreate():  Fires when form is opened. Perform initialization data here
               before form is displayed and ready to use.
*******************************************************************************)
procedure TfrmEngDb.FormCreate(Sender: TObject);
begin
  //Connect to the database
  if not connEngDb.Connected then
    try
      connEngDb.Open();
    except
       ShowMessage('Unable to open Engineering database.');
    end;
  if not tblTask.Active then
    try
      tblTask.Open;
    except
      ShowMessage('Unable to open table TASK.');
    end;
  if not tblSubTask.Active then
    try
      tblSubTask.Open;
    except
      ShowMessage('Unable to open table SUBTASK.');
    end;
  if not tblPrj.Active then
    try
      tblPrj.Open;
    except
      ShowMessage('Unable to open table PROJECT.');
    end;
  if not tblPrjTask.Active then
    try
      tblPrjTask.Open;
    except
      ShowMessage('Unable to open table PRJTASK.');
    end;
  if not tblReport.Active then
    try
      tblReport.Open;
    except
      ShowMessage('Unable to open table REPORT.');
    end;
  if not tblResource.Active then
    try
      tblResource.Open;
    except
      ShowMessage('Unable to open table RESOURCE.');
    end;
  //Initialize tab sheet panels to not visible
  SubTaskVisible(False);
  TaskVisible(False);
  PrjVisible(False);
  PrjTaskVisible(False);
  ResourceVisible(False);
  //Init tables to specify dynamic columns (as displayed in corresponding DBGrid), and set min width
  tblTask.FieldByName('DESCRIPTION').Tag := 250;   //min width of 250 in Grid
  tblPrj.FieldByName('DESCRIPTION').Tag := 250;
  tblPrjTask.FieldByName('DESCRIPTION').Tag := 250;
  tblSubTask.FieldByName('DESCRIPTION').Tag := 250;

end;

(*******************************************************************************
  FormResize(): Need to manage the size/position of the grids, data entry panels
                etc when the primary form is resized.
*******************************************************************************)
procedure TfrmEngDb.FormResize(Sender: TObject);
begin
  case PageControl1.ActivePageIndex of
  0:
    begin
      bevPrj.Top := tsProjects.Height div 2;
      dbGridPrj.Height := bevPrj.Top - dbGridPrj.Top - 8;
      cbViewPrjTaskControls.Top := bevPrj.Top + 5;
      Label47.Top := bevPrj.Top + 5;
      if pnlPrjTask.Visible then
        begin
         pnlPrjTask.Top := bevPrj.Top + 20;
         dbGridPrjTask.Top := bevPrj.Top + 171;
         dbGridPrjTask.Height := tsProjects.Height - bevPrj.Top - 171;
        end
        else
          begin
            dbGridPrjTask.Top := bevPrj.Top + 20;
            dbGridPrjTask.Height := tsProjects.Height - bevPrj.Top - 20;
          end;
    end;
    1:
    begin
      bevTask.Top := tsTasks.Height div 2;
      dbGridTasks.Height := bevTask.Top - dbGridTasks.Top - 8;
      cbViewSubTaskControls.Top := bevTask.Top + 5;
      Label8.Top := bevTask.Top + 5;
      if pnlSubTask.Visible then
        begin
         pnlSubTask.Top := bevTask.Top + 20;
         dbGridSubTasks.Top := bevTask.Top + 171;
         dbGridSubTasks.Height := tsTasks.Height - bevTask.Top - 171;
        end
        else
          begin
            dbGridSubTasks.Top := bevTask.Top + 20;
            dbGridSubTasks.Height := tsTasks.Height - bevTask.Top - 20;
          end;
    end;
  end;
end;

(*_procedure TfrmEngDb.FormShow(Sender: TObject);
begin

end;

____________________________________________________________________________

                 MULTIPLE TABLE RELATED
______________________________________________________________________________*)
(******************************************************************************
  DBOwnerDropDown():  Fires when the OWNER control is dropped down. Populates
                      with items from the RESOURCE table.
*******************************************************************************)
procedure TfrmEngDb.DBOwnerDropDown(Sender: TObject);
var
  Drop: TDBComboBox; i: Integer;
begin
  Drop := Sender as TDBComboBox;
  tblResource.First;   //go to first record in Resource table
  for i := 1 to tblResource.RecordCount-1 do
    begin
      Drop.Items.Add(tblResource.FieldByName('FIRST_NAME').AsString + ' ' +
                     tblResource.FieldByName('LAST_NAME').AsString);
      tblResource.Next;
    end;
end;
(******************************************************************************
  DBOwnerExit():  Fires when the OWNER control is exited. Ensures that the name
                  selected exits. If not, prompts user to create resource.
*******************************************************************************)
procedure TfrmEngDb.DBOwnerExit(Sender: TObject);
var
  Drop: TDBComboBox; i: Integer;
begin
  Drop := Sender as TDBComboBox;
  tblResource.First;
  if Drop.Text <> 'Unassigned' then
    if not tblResource.Locate('FULL_NAME',Drop.Text,[]) then
      begin
        ShowMessage('Resource does not exist. Please add to Resources table ' +
                    #13 + 'before assigning task or project.');
        Drop.DataSource.DataSet.Cancel;
      end;
end;
(*******************************************************************************
  dsStateChange():  Call from any data set's OnStateChange event to obtain a string
                  showing the current dataset state.
*******************************************************************************)
function TfrmEngDb.dsStateChange(Sender: TObject):string;
var
  tmp: string; ds: TDataSource;
begin
  ds := Sender as TDataSource;
  case ds.State of
    dsInactive:  tmp := 'Inactive';
    dsBrowse:  tmp := 'Browse';
    dsEdit: tmp := 'Edit';
    dsInsert: tmp := 'Insert'
    else
      tmp := 'Other';
  end;
  result := tmp;
end;

(*_____________________________________________________________________________

                 TASK TABLE RELATED
______________________________________________________________________________*)
(******************************************************************************
  dbGridTasksTitleClick():  Fires when the title bar of the grid is clicked.
                              Sorts the grid based on the column field selected.
*******************************************************************************)
procedure TfrmEngDb.dbGridTasksTitleClick(Column: TColumn);
begin
  DBGridTitleClick(dbGridTasks, Column);
end;
(*******************************************************************************
  dsTaskStateChange():  Fires when the Data Source changes state- used to determine
                        the mode (eg dsInsert, dsBrowse etc.) of the corresponding
                        data set.
*******************************************************************************)
procedure TfrmEngDb.dsTaskStateChange(Sender: TObject);
begin
  lblTaskTblState.Caption := dsStateChange(Sender);
end;

(*******************************************************************************
  dtTaskDueChange():  Use event to save Edit, if ds is in Edit mode.
*******************************************************************************)
procedure TfrmEngDb.dtTaskDueChange(Sender: TObject);
begin
  if dsTask.State = dsEdit then
    begin
      tblTask['DUE_DATE'] := dtTaskDue.DateTime;
      tblTask.Post;
    end;
end;
(*******************************************************************************
  dtTaskDueEnter():  Use event as "Auto Edit" for a non-data aware date picker.
*******************************************************************************)
procedure TfrmEngDb.dtTaskDueEnter(Sender: TObject);
begin
  if dsTask.State = dsBrowse then
    tblTask.Edit;
end;
(*******************************************************************************
  TaskVisible():  Configure the sub-task grid/data controls based on the
                     state of the "View Controls" check box.
*******************************************************************************)
procedure TfrmEngDb.TaskVisible(state: Boolean);
begin
  if state = False then
    begin
      pnlTask.Visible := False;
      dbGridTasks.Top := tsTasks.Top + 3;
      dbGridTasks.Height := bevPrj.Top - 34;
    end
    else
      begin
        pnlTask.Top := tsTasks.Top + 3;
        dbGridTasks.Top := pnlTask.Top + pnlTask.Height + 10;
        dbGridTasks.Height := bevPrj.Top - (pnlTask.Top + pnlTask.Height +16);
        pnlTask.Visible := True;
      end;
end;

(******************************************************************************
  tblTaskAfterInsert():  Fires when Task table mode is set to INSERT. Used to
                         initialize Created Date value.
*******************************************************************************)
procedure TfrmEngDb.tblTaskAfterInsert(DataSet: TDataSet);
begin
  tblTask['CREATION_DATE'] := Now;
  tblTask['DUE_DATE'] := Now;
end;
(*******************************************************************************
  tblTaskAfterScroll():  Fires anytime a the current record is scrolled to the
                         next/previous etc. Good place to update non-data aware
                         controls.
*******************************************************************************)
procedure TfrmEngDb.tblTaskAfterScroll(DataSet: TDataSet);
begin
  dtTaskDue.DateTime := tblTask.FieldByName('DUE_DATE').AsDateTime;
end;
(*******************************************************************************
  tblTaskBeforePost():  Using to populate DUE DATE from date picker into tbl.
*******************************************************************************)
procedure TfrmEngDb.tblTaskBeforePost(DataSet: TDataSet);
begin
  tblTask['DUE_DATE'] := dtTaskDue.DateTime;
end;

procedure TfrmEngDb.tsTasksResize(Sender: TObject);
begin
  FixDBGridColumnsWidth(dbGridTasks);
  FixDBGridColumnsWidth(dbGridSubTasks);
end;


procedure TfrmEngDb.ViewProjectControls1Click(Sender: TObject);
begin
  if ViewProjectControls1.Visible then
    PrjVisible(True)
      else
        PrjVisible(False);
end;

(*******************************************************************************
  dbGridTasksMouseMove):  Respond to movement of the mouse over the "Task" DB
                          grid.
*******************************************************************************)
procedure TfrmEngDb.dbGridTasksMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  DBGridMouseMove(dbGridTasks, Sender, Shift, X, Y);
end;
(*******************************************************************************
  dbGridTasksDrawColumnCell():  Draw rows in alternating colors for readability,
                                or if task is past due, draw in different color
                                to make user aware.
*******************************************************************************)
procedure TfrmEngDb.dbGridTasksDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  DBGridDrawRows(Sender, Rect, DataCol, Column, State);
end;

(*_____________________________________________________________________________

                 SUB TASK TABLE RELATED
______________________________________________________________________________*)
(*******************************************************************************
  lblSubTaskStateChange():  Update label with current status of data set.
*******************************************************************************)
procedure TfrmEngDb.dsSubTaskStateChange(Sender: TObject);
begin
  lblSubTaskState.Caption := dsStateChange(Sender);
end;
(*******************************************************************************
  dtSubTaskDueChange():  Use event to save Edit, if ds is in Edit mode.
*******************************************************************************)
procedure TfrmEngDb.dtSubTaskDueChange(Sender: TObject);
begin
  if dsSubTask.State = dsEdit then
    begin
      tblSubTask['DUE_DATE'] := dtSubTaskDue.DateTime;
      tblSubTask.Post;
    end;
end;
(*******************************************************************************
  dtSubTaskDueEnter():  Use event as "Auto Edit" for a non-data aware date picker.
*******************************************************************************)
procedure TfrmEngDb.dtSubTaskDueEnter(Sender: TObject);
begin
  if dsSubTask.State = dsBrowse then
    tblSubTask.Edit;
end;

(******************************************************************************
  tbl_SubTaskAfterInsert():  Fires when SubTask table mode is set to INSERT. Used to
                         initialize Created Date value.
*******************************************************************************)
procedure TfrmEngDb.tblSubTaskAfterInsert(DataSet: TDataSet);
begin
  tblSubTask['CREATION_DATE'] := Now;
  tblSubTask['DUE_DATE'] := Now;
end;
(*******************************************************************************
  tbl_SubTaskAfterScroll():  Fires anytime a the current record is scrolled to the
                         next/previous etc. Good place to update non-data aware
                         controls.
*******************************************************************************)
procedure TfrmEngDb.tblSubTaskAfterScroll(DataSet: TDataSet);
begin
  dtSubTaskDue.DateTime := tblSubTask.FieldByName('DUE_DATE').AsDateTime;
end;
(*******************************************************************************
  tbl_SubTaskBeforePost():  Using to populate DUE DATE from date picker into tbl.
*******************************************************************************)
procedure TfrmEngDb.tblSubTaskBeforePost(DataSet: TDataSet);
begin
  tblSubTask['DUE_DATE'] := dtSubTaskDue.DateTime;
end;
(*******************************************************************************
  SubTaskVisible():  Configure the sub-task grid/data controls based on the
                     state of the "View Controls" check box.
*******************************************************************************)
procedure TfrmEngDb.SubTaskVisible(state: Boolean);
begin
  if state = False then
    begin
      pnlSubTask.Visible := False;
      dbGridSubTasks.Top := bevPrj.Top + bevPrj.Height +26;
      dbGridSubTasks.Height := tsTasks.Height - dbGridSubTasks.Top - 10;
    end
    else
      begin
        pnlSubTask.Top := bevPrj.Top + bevPrj.Height +26;
        dbGridSubTasks.Top := pnlSubTask.Top + pnlSubTask.Height + 10;
        dbGridSubTasks.Height := tsTasks.Height - dbGridSubTasks.Top - 10;
        pnlSubTask.Visible := True;
      end;
end;


(*******************************************************************************
  dbGridSubTasksMouseMove):  Respond to movement of the mouse over the "SubTask" DB
                          grid.
*******************************************************************************)
procedure TfrmEngDb.dbGridSubTasksMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  DBGridMouseMove(dbGridSubTasks, Sender, Shift, X, Y);
end;

(******************************************************************************
  dbGridSubTasksTitleClick():  Fires when the title bar of the grid is clicked.
                              Sorts the grid based on the column field selected.
*******************************************************************************)
procedure TfrmEngDb.dbGridSubTasksTitleClick(Column: TColumn);
begin
  DBGridTitleClick(dbGridSubTasks, Column);
end;
(*******************************************************************************
  dbGridSubTasksDrawColumnCell(): Draw rows in alternating colors for readability,
                                or if task is past due, draw in different color
                                to make user aware.
*******************************************************************************)
procedure TfrmEngDb.dbGridSubTasksDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  DBGridDrawRows(Sender, Rect, DataCol, Column, State);
end;
(*______________________________________________________________________________

         PROJECT TBL RELATED
_______________________________________________________________________________*)
procedure TfrmEngDb.cbFilterClick(Sender: TObject);
begin
  if cbFilter.Checked then
    begin
      tblPrj.Filtered := False;
      tblPrj.Filter := 'OWNER LIKE '+QuotedStr(edFilter.Text);
      tblPrj.Filtered := True;
    end
    else
      begin
        edFilter.Text := '';
        tblPrj.Filtered := False;
      end;
end;


procedure TfrmEngDb.dbGridPrjDblClick(Sender: TObject);
begin
  if not pnlPrj.Visible then
    PrjVisible(True);
  frmEngDb.Resize;
end;

(*******************************************************************************
  lblProjectControlsExitClick():  Hide the Project Table controls.
*******************************************************************************)
procedure TfrmEngDb.lblProjectControlsExitClick(Sender: TObject);
begin
  PrjVisible(False);
  frmEngDb.Resize;
end;
(*******************************************************************************
  dsPrjStateChange():  Update label with current status of data set.
*******************************************************************************)
procedure TfrmEngDb.dsPrjStateChange(Sender: TObject);
begin
  lblPrjState.Caption := dsStateChange(Sender);
end;
(*******************************************************************************
  tblPrjBeforePost():  Using to populate DUE DATE from date picker into tbl.
*******************************************************************************)
procedure TfrmEngDb.tblPrjTaskBeforePost(DataSet: TDataSet);
begin
  tblPrjTask['DUE_DATE'] := dtPrjTaskDue.DateTime;
end;

(*******************************************************************************
  dbGridPrjMouseMove():  Respond to movement of the mouse over the "PRJ" DB
                          grid.
*******************************************************************************)
procedure TfrmEngDb.dbGridPrjMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  DBGridMouseMove(dbGridPrj, Sender, Shift, X, Y);
end;

(*******************************************************************************
  dtPrjDueChange():  Use event to save Edit, if ds is in Edit mode.
*******************************************************************************)
procedure TfrmEngDb.dtPrjDueChange(Sender: TObject);
begin
  if dsPrj.State = dsEdit then
    begin
      tblPrj['DUE_DATE'] := dtPrjDue.DateTime;
      tblPrj.Post;
    end;
end;
(*******************************************************************************
  dtPrjDueEnter():  Use event as "Auto Edit" for a non-data aware date picker.
*******************************************************************************)
procedure TfrmEngDb.dtPrjDueEnter(Sender: TObject);
begin
  if dsPrj.State = dsBrowse then
    tblPrj.Edit;
end;
(******************************************************************************
  tblPrjAfterInsert():  Fires when Prj table mode is set to INSERT. Used to
                         initialize Created Date value.
*******************************************************************************)
procedure TfrmEngDb.tblPrjAfterInsert(DataSet: TDataSet);
begin
  tblPrj['CREATION_DATE'] := Now;
  tblPrj['DUE_DATE'] := Now;
end;
(*******************************************************************************
  tblPrjAfterScroll():  Fires anytime a the current record is scrolled to the
                         next/previous etc. Good place to update non-data aware
                         controls.
*******************************************************************************)
procedure TfrmEngDb.tblPrjAfterScroll(DataSet: TDataSet);
begin
  dtPrjDue.DateTime := tblPrj.FieldByName('DUE_DATE').AsDateTime;
end;
(*******************************************************************************
  tblPrjBeforePost():  Using to populate DUE DATE from date picker into tbl.
*******************************************************************************)
procedure TfrmEngDb.tblPrjBeforePost(DataSet: TDataSet);
begin
  tblPrj['DUE_DATE'] := dtPrjDue.DateTime;
end;
(*******************************************************************************
  PrjVisible():  Configure the sub-task grid/data controls based on the
                     state of the "View Controls" check box.
*******************************************************************************)
procedure TfrmEngDb.PrjVisible(state: Boolean);
begin
  if state = False then
    begin
      pnlPrj.Visible := False;
      dbGridPrj.Top := tsProjects.Top + 3;
      dbGridPrj.Height := bevTask.Top - 34;
    end
    else
      begin
        pnlPrj.Top := tsProjects.Top + 3;
        dbGridPrj.Top := pnlPrj.Top + pnlPrj.Height + 10;
        dbGridPrj.Height := bevTask.Top - (pnlPrj.Top + pnlPrj.Height +16);
        pnlPrj.Visible := True;
      end;
end;
(******************************************************************************
  dbGridPrjTitleClick():  Fires when the title bar of the grid is clicked.
                              Sorts the grid based on the column field selected.
*******************************************************************************)
procedure TfrmEngDb.dbGridPrjTitleClick(Column: TColumn);
begin
  DBGridTitleClick(dbGridPrj, Column);
end;

(*******************************************************************************
  dbGridPrjDrawColumnCell():  Draw rows in alternating colors for readability,
                                or if task is past due, draw in different color
                                to make user aware.
*******************************************************************************)
procedure TfrmEngDb.dbGridPrjDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  DBGridDrawRows(Sender, Rect, DataCol, Column, State);
end;


(*______________________________________________________________________________

     PROJECT TASK TBL RELATED
_______________________________________________________________________________*)
(*******************************************************************************
  dsPrjTaskStateChange():  Update label with current status of data set.
*******************************************************************************)
procedure TfrmEngDb.dsPrjTaskStateChange(Sender: TObject);
begin
  lblPrjTaskState.Caption := dsStateChange(Sender);
end;
(*******************************************************************************
  dbGridPrjMouseMove():  Respond to movement of the mouse over the "PRJ" DB
                          grid.
*******************************************************************************)
procedure TfrmEngDb.dbGridPrjTaskMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  DBGridMouseMove(dbGridPrjTask, Sender, Shift, X, Y);
end;
(*******************************************************************************
  dbGridPrjTaskDrawColumnCell():  Draw rows in alternating colors for readability,
                                or if task is past due, draw in different color
                                to make user aware.
*******************************************************************************)
procedure TfrmEngDb.dbGridPrjTaskDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  DBGridDrawRows(Sender, Rect, DataCol, Column, State);
end;
(*******************************************************************************
  dtPrjTaskDueChange():  Use event to save Edit, if ds is in Edit mode.
*******************************************************************************)
procedure TfrmEngDb.dtPrjTaskDueChange(Sender: TObject);
begin
  if dsPrjTask.State = dsEdit then
    begin
      tblPrjTask['DUE_DATE'] := dtPrjTaskDue.DateTime;
      tblPrjTask.Post;
    end;
end;
(*******************************************************************************
  dtPrjDueEnter():  Use event as "Auto Edit" for a non-data aware date picker.
*******************************************************************************)
procedure TfrmEngDb.dtPrjTaskDueEnter(Sender: TObject);
begin
  if dsPrjTask.State = dsBrowse then
    tblPrjTask.Edit;
end;
(******************************************************************************
  tblPrjTaskAfterInsert():  Fires when Prj table mode is set to INSERT. Used to
                         initialize Created Date value.
*******************************************************************************)
procedure TfrmEngDb.tblPrjTaskAfterInsert(DataSet: TDataSet);
begin
  tblPrjTask['CREATION_DATE'] := Now;
  tblPrjTask['DUE_DATE'] := Now;
end;
(*******************************************************************************
  tblPrjTaskAfterScroll():  Fires anytime a the current record is scrolled to the
                         next/previous etc. Good place to update non-data aware
                         controls.
*******************************************************************************)
procedure TfrmEngDb.tblPrjTaskAfterScroll(DataSet: TDataSet);
begin
  if dsPrjTask.State <> dsInsert then
     dtPrjTaskDue.DateTime := tblPrjTask.FieldByName('DUE_DATE').AsDateTime
     else
       dtPrjTaskDue.DateTime := Now;
end;
(******************************************************************************
  dbGridPrjTaskTitleClick():  Fires when the title bar of the grid is clicked.
                              Sorts the grid based on the column field selected.
*******************************************************************************)
procedure TfrmEngDb.dbGridPrjTaskTitleClick(Column: TColumn);
begin
  DBGridTitleClick(dbGridPrjTask, Column);
end;

(*******************************************************************************
  ViewPrjTaskControlsClick():  Determine the visual state of the SubTask
                               Controls and display accordingly.
*******************************************************************************)
procedure TfrmEngDb.cbViewPrjTaskControlsClick(Sender: TObject);
begin
  if cbViewPrjTaskControls.Checked then
    PrjTaskVisible(True)
      else
        PrjTaskVisible(False);
  frmEngDb.Resize;
end;
(*******************************************************************************
  PrjTaskVisible():  Configure the sub-task grid/data controls based on the
                     state of the "View Controls" check box.
*******************************************************************************)
procedure TfrmEngDb.PrjTaskVisible(state: Boolean);
begin
  if state = False then
    begin
      pnlPrjTask.Visible := False;
      dbGridPrjTask.Top := bevTask.Top + bevTask.Height +26;
      dbGridPrjTask.Height := tsProjects.Height - dbGridPrjTask.Top - 10;
    end
    else
      begin
        pnlPrjTask.Top := bevTask.Top + bevTask.Height +26;
        dbGridPrjTask.Top := pnlPrjTask.Top + pnlPrjTask.Height + 10;
        dbGridPrjTask.Height := tsProjects.Height - dbGridPrjTask.Top - 10;
        pnlPrjTask.Visible := True;
      end;
end;
(*******************************************************************************
  PageControl1Resize():  Responsds to size changes in the page control resulting
                         from size changes of the Master form. Adjusts the column
                         widths so that as the page is expanded, the DBGrids
                         do not have left over space.
******************************************************************************)
procedure TfrmEngDb.PageControl1Resize(Sender: TObject);
begin
  FixDBGridColumnsWidth(dbGridPrj);
  FixDBGridColumnsWidth(dbGridPrjTask);
end;

(*______________________________________________________________________________

                              RESOURCES TABLE RELATED
______________________________________________________________________________*)

procedure TfrmEngDb.dsRepUpdateData(Sender: TObject);
begin
  if dbGridPrj.Columns.Count > 3 then
     begin
       dbGridPrj.Columns[2].Width := 240;
       dbGridPrj.Columns[8].Width := 240;
     end;
end;

(*******************************************************************************
  dsResourceStateChange():  Update label with current status of data set.
*******************************************************************************)
procedure TfrmEngDb.dsResourceStateChange(Sender: TObject);
begin
  lblResourceState.Caption := dsStateChange(Sender);
end;

(*******************************************************************************
  ResourceVisible():  Configure the Resources grid/data controls based on the
                     state of the "View Controls" check box.
*******************************************************************************)
procedure TfrmEngDb.ResourceVisible(state: Boolean);
begin
  if state = False then
    begin
      pnlResource.Visible := False;
      dbGridResource.Top := tsResources.Top + 5;
      dbGridResource.Height := tsResources.Height - 10;
    end
    else
      begin
        pnlResource.Top := tsResources.Top + 5;
        dbGridResource.Top := pnlResource.Top + pnlResource.Height + 10;
        dbGridResource.Height := tsResources.Height - (pnlTask.Height +30);
        pnlResource.Visible := True;
      end;
end;

(*******************************************************************************
  ViewCtrlsResourcesClick():  Determine the visual state of the SubTask
                               Controls and display accordingly.
*******************************************************************************)
procedure TfrmEngDb.cbViewCtrlsResourcesClick(Sender: TObject);
begin
  if cbViewCtrlsResources.Checked then
    ResourceVisible(True)
      else
        ResourceVisible(False);
end;
(*******************************************************************************
  dbGridResourceDrawColumnCell():  Draw rows in alternating colors for readability,
***************************************************************************)
procedure TfrmEngDb.dbGridResourceDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  DBResDrawRows(Sender, REct, DataCol, Column, State);
end;
(******************************************************************************
  dbGridResourceTitleClick():  Fires when the title bar of the grid is clicked.
                              Sorts the grid based on the column field selected.
*******************************************************************************)
procedure TfrmEngDb.dbGridResourceTitleClick(Column: TColumn);
begin
   DBGridTitleClick(dbGridResource, Column);
end;

(*******************************************************************************
   GENERIC CLICK PROCEDURE:  Attach to a button to automatically populate fields
                             per code specific instructions. Leaving in as an
                             option until initial data is all in place.
*******************************************************************************)
(*procedure TfrmEngDb.Button1.Click(Sender: TObject);
begin
  if tblResource.State <> dsEdit then
    tblResource.Edit;
  tblResource.First;
  while not tblResource.Eof  do
    begin
      if tblResource.State <> dsEdit then
        tblResource.Edit;
      tblResource['COMPANY'] := 'Merit Medical';
      tblResource['DEPT'] := '210 ENG';
      tblResource.Next;
    end;
end; *)

(*______________________________________________________________________________

                              REPORT TAB SHEET RELATED
______________________________________________________________________________*)

(*******************************************************************************
  cbShowReportControlsClick():  Display or hide the Report edit panel.
*******************************************************************************)
procedure TfrmEngDb.cbShowReportControlsClick(Sender: TObject);
begin
  if cbShowReportControls.Checked then
    begin
      dsRep.Enabled := False;
      memSqlTest.Visible := False;
      pnlReportControls.Visible := True;
      dbGridRep.DataSource := dsReportTbl;
      dsReportTbl.Enabled := True;
    end
    else
      begin
        dsReportTbl.Enabled := False;
        pnlReportControls.Visible := False;
        memSqlTest.Visible := True;
        dbGridRep.DataSource := dsRep;
        dsRep.Enabled := True;
      end;
end;

(*******************************************************************************
  btnSqlClick():  Execute the SQL query, and generate corresponding report
*******************************************************************************)
procedure TfrmEngDb.btnSqlClick(Sender: TObject);
begin
  //dsRep.Enabled := False;
  //dbGridRep.DataSource := dsRep;
  qRep.SQL.Clear;
  qRep.Close;
  qRep.SQL.AddStrings(memSqlTest.Lines);
  qRep.Open;
  //dsRep.Enabled := True;
end;

(*******************************************************************************
  btnRGetPath.Click():  Configure the path to an EngDB Report
*******************************************************************************)
procedure TfrmEngDb.btnGetPathClick(Sender: TObject);
begin
  if dlgReportOpen.Execute then
    begin
      if tblReport.State <> dsEdit then
        tblReport.Edit;
      edReportLocation.Text := ExtractFilePath(dlgReportOpen.FileName);
    end;
end;
(*******************************************************************************
  btnNewReport.Click():  Launch the Fast Report design utility.
*******************************************************************************)
procedure TfrmEngDb.btnNewReportClick(Sender: TObject);
begin
  frxReport1.DesignReport(True);
end;

(*******************************************************************************
  btnRefreshRpt.Click():  Rerun the report after updating the SQL query.
*******************************************************************************)
procedure TfrmEngDb.btnRefreshRptClick(Sender: TObject);
begin
   if edReportName.Text <> '' then
      frxReport1.ShowReport(True);
end;
(*******************************************************************************
  btnReport.Click():  Launch the selected report.
*******************************************************************************)
procedure TfrmEngDb.btnReportClick(Sender: TObject);
begin
  if edReportName.Text <> '' then
    begin
      if not frxReport1.LoadFromFile(tblReport.FieldByName('RPTPATH').AsString +
                                     tblReport.FieldByName('RPTNAME').AsString) then
        ShowMessage('Error loading report.')
        else
          begin
            memSql.Clear;
            memSql.Text := tblReport.FieldByName('SQLSTRING').AsString;
            qRep.SQL.Clear;
            qRep.Close;
            qRep.SQL.AddStrings(memSql.Lines);
            qRep.Open;
            frxReport1.ShowReport(True);
          end;
    end
    else
      ShowMessage('Select valid report.');
end;

(*******************************************************************************
  btnSaveSql.Click():  Save the current SQL window contents to the current
                       report table record.
*******************************************************************************)
procedure TfrmEngDb.btnSaveSqlClick(Sender: TObject);
var
  i: Integer;
begin
  if MessageDlg('This will permanently change this SQL query. '+#13+
                 'Are you sure?', mtConfirmation, [mbYes, mbNo],0, mbNo)= mrYes  then
     begin
       if memSQL.Lines[0] <> '' then
         begin
           if tblReport.State <> dsEdit then
             tblReport.Edit;
           tblReport['SQLSTRING'] := '';
           for i := 0 to memSQL.Lines.Count - 1 do
             tblReport['SQLSTRING'] := tblReport['SQLSTRING'] + memSQL.Lines[i] +' ';
             tblReport.Post;
           end;
     end;
end;


end.
